<?php
declare (strict_types = 1);

namespace app\model;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class Blogtype extends Model
{
    protected $pk ='tid';
    //获取栏目列表
    public static function getNavList(){
        return Blogtype::select();
    }
    //获取分类数据列表
    public function categoryList(){
        return $this->cate(Blogtype::select());
    }
    //获取无限级分类
    public function cate($arrs,$level=0,$sid=0)	{
        static $categorys = array();
        //循环原始的分类数组
        foreach($arrs as $arr)
        {
            //查找下级菜单
            if($arr['sid']==$sid)
            {
                $arr['level'] = $level;
                $arr['html']=str_repeat('--',$level*4);
                $categorys[] = $arr;
                //递归调用
                $this->cate($arrs,$level+1,$arr['tid']);
            }
        }
        //返回结果
        return $categorys;
    }
    //递归获取所有的子菜单的ID
    function get_all_child($array,$tid){
        $arr = array();
        foreach($array as $v){
            if($v['sid'] === $tid){
                $arr[] = $v['tid'];
                $this->get_all_child($array,$v['tid']);
            };
        };
        return $arr;
    }
    //获取无限极栏目数组--递归
    public static function navList(){
        $arrs = Blogtype::select();
        foreach ($arrs as $v){
            if($v['sid']==0){
                $topArrs[] = $v;
            }
            if($v['sid']!=0){
                $subordinateArrs[]=$v;
            }
        }
        return json_encode($subordinateArrs);
    }
}
